package com.dy.drp.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dy.drp.entity.Provider;
import com.dy.drp.entity.User;
import com.dy.drp.service.IProviderService;
import com.dy.drp.service.IUserService;
import com.dy.drp.util.SelectUtil;
import com.dy.drp.vo.UserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dy
 * @since 2022-04-01
 */
@RestController
@RequestMapping("/provider")
@Api(tags="供应商管理接口")
public class ProviderController {
    @Autowired
    private IProviderService providerService;

    @GetMapping("/selectProvider/{pageNum}/{pageSize}")
    @ApiOperation("获取单页供应商信息")
    public IPage<Provider> selectProvider(@PathVariable("pageNum")Integer pageNum,
                                          @PathVariable("pageSize")Integer pageSize){
        Page<Provider> page = new Page(pageNum,pageSize);
        return  providerService.page(page);
    }

    @PostMapping("/insertProvider")
    @ApiOperation("添加供应商")
    public String insertProvider(Provider provider){
        if(providerService.save(provider)){
            return "供应商添加成功";
        }
        return "供应商添加失败";
    }

    @PostMapping("/updateProvider")
    @ApiOperation("修改供应商信息")
    public String updateProvider(Provider provider){
        QueryWrapper<Provider> wrapper = new QueryWrapper<>();
        wrapper.eq("provider_name",provider.getProviderName());
        if(providerService.update(provider,wrapper)){
            return "修改成功";
        }
        return "修改失败";
    }

    @GetMapping("/selectOneProvider/{providerName}")
    @ApiOperation("查看供应商信息")
    @ApiImplicitParam(name="providerName",value = "供应商名称")
    public Provider selectProviderByName(@PathVariable("providerName")String providerName){
        QueryWrapper<Provider> wrapper = new QueryWrapper<>();
        wrapper.eq("provider_name",providerName);
        return providerService.getOne(wrapper);
    }

    @PostMapping("/selectByNameAndScope")
    @ApiOperation("通过供应商名字和经营范围查询供应商信息")
    public Page<Provider> selectByNameAndScope(SelectUtil selectUtil){
        QueryWrapper<Provider> wrapper = new QueryWrapper<>();
        wrapper.like("provider_name",selectUtil.getName());
        wrapper.like("provider_scope",selectUtil.getStartTime());
        Page<Provider> page = new Page<>(selectUtil.getPageNum(),selectUtil.getPageSize());
        return providerService.page(page, wrapper);
    }

    @GetMapping("/selectProviderNames")
    @ApiOperation("获取所有供应商名")
    public List<Provider> selectProvider(){
        return  providerService.list();
    }
}
